<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div class="easyui-layout" style="width:100%;height:600px;">
			<div data-options="region:'center'">
				<div>
					<table id="tt" style="width: 100%;" class="table_style">
					</table>
				</div>
			</div>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/contractShareFunction.js"></script>
	</body>
	<script>
		var currid = getID();
		var content = "";
		var AddDialog;
		var _detailToolbarData;
		var cardDetail = new Array(); //用来存储卡证的信息
		var index = 0; //用来标志cardDetail数组的下标
		var auth = "admin;" + hex_sha1("admin1");
		var DialogWidth = 900;
		var DialogHeight = 470;
		_detailToolbarData = AddDetailToolBar(7);

		var data = [{
			"icon": "icon-save",
			"func": "save()",
			"functionname": "保存"
		}];

		content = AddReceiptsToolBar(0, data);
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		getDataGrid();
		getRemoteData();

		function newdata() {
			var pLocation = "BusinessInformationAdd.html?id=-1";
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "固定商户新增", DialogWidth, DialogHeight);
			AddDialog.dialog('open');
		}

		function loaddata() {
			$.messager.confirm('Confirm', '是否导出', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/tenant/excel.do", {
							AUTHORIZATION: auth
						},
						function(result) {
							if(result.msg == "success") {
								_downloadpath = result.path;
								_pathTarget = "<a href=\'" + _downloadpath + "\' target='_blank'>点击下载</a>";
								$.messager.alert({ // show error message
									title: '成功',
									msg: _pathTarget
								});
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});

		}

		function editdata() {
			var row = $("#dg").datagrid("getSelected");
			if(!row) {
				$.messager.alert("消息", "请选中数据");
				return;
			}
			var pLocation = "BusinessInformationAdd.html?id=" + row.ID;
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "固定商户修改", DialogWidth, DialogHeight);
			AddDialog.dialog('open');
		}

		function redodata() {
			$('#dg').datagrid('options').url = RemoteLinkAddress + '/market/tenant/list.do?TYPE=0';
			$('#dg').datagrid('reload');
		}

		function print() {
			var row = $('#dg').datagrid('getSelected');
			if(!row) {
				$.messager.alert('Info', "没有选中");
			}
			var pLocation = "BusinessInformationPrint.html?id=" + row.ID;
			window.open(pLocation);
		}

		function getDataGrid() {
			$('#tt').edatagrid({
				method: 'get',
				toolbar: _detailToolbarData,
				title: '卡证及责任书明细',
				pagination: 'true',
				rownumbers: 'true',
				fitColumns: 'false',
				singleSelect: 'true',
				autoSave: true,
				height: 350,
				columns: [
					[{
						field: 'BOOST_ID',
						title: 'BOOST_ID',
						width: '0%'
					}, {
						field: 'HOUSE_NUMBER',
						title: '摊位门牌号',
						width: '24%',
						align: 'center'
					}, {
						field: 'BUSINESS_AREA',
						title: '经营面积',
						width: '24%',
						align: 'center'
					}, {
						field: 'NAME',
						title: '责任书',
						width: '24%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								url: RemoteLinkAddress + "/market/duty/listall.do",
								valueField: "NAME",
								textField: "NAME",
								multiple: true,
								editable: true,
								required: true,
							}
						}
					}, {
						field: 'CARDNAME',
						title: '卡证',
						width: '24%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
//								url: RemoteLinkAddress + "/market/boostcard/listall.do",
								valueField: "NAME",
								textField: "NAME",
								multiple: true,
								editable: true,
								required: false,
								onSelect: function(record) {
									//									console.log(record);
									cardDetail[index] = record;
									index++;
								},
								onUnselect: function(record) {
									for(var i = 0; i < cardDetail.length; i++) {
										if(typeof(cardDetail[i]) != "undefined") {
											if(typeof(cardDetail[i].ID) != "undefined") {
												if(record.ID == cardDetail[i].ID) {
													cardDetail.splice(i, 1);
												}
											}
										}
									}
								}
							}
						}
					}]
				],
				onDblClickRow: function(index, row) {
					var rowIndex = $("#tt").datagrid('getRowIndex', row);
					var target = $('#tt').datagrid('getEditor', {
						'index': rowIndex,
						'field': 'CARDNAME'
					}).target;
					target.combobox('clear');
					var url = RemoteLinkAddress + "/market/boostcard/listall.do?BOOST_ID=" + row.BOOST_ID;
					target.combobox('reload', url);
				},
			});
			$('#tt').datagrid('hideColumn', 'BOOST_ID'); //隐藏第一列
		}

		function getRemoteData() //统一获取远程的数据
		{
			var data = JSON.parse(localStorage.longTermContractData);
			var rowsdata = data.rowsBoost;
			$("#tt").datagrid('loadData', rowsdata);
			console.log(data);
		}

		function save() {
			getData();
		}

		function getData() {
			var totalData = JSON.parse(localStorage.longTermContractData);
			totalData.rowsCard = new Array();
			totalData.rowsDuty = new Array();
			var dataGridRows = $("#tt").datagrid("getRows");
			console.log(dataGridRows);
			totalData = rebuildCardAndDutyData(dataGridRows, totalData, cardDetail);
			localStorage.longTermContractData = JSON.stringify(totalData);
			var totalCharge = getTotalCharge(totalData);
			console.log(totalCharge);
			totalData.SUM = totalCharge;
			submitData(totalData);
			console.log(totalData);
		}

		function submitData(_data) {
			var _url = "";
			if(currid == AddFlag){
				_url = RemoteLinkAddress + "/market/contract/save.do";
			}else{
				_url = RemoteLinkAddress + "/market/contract/update.do";
				_data.ID = currid;
			}
			//			console.log(_data);
			var _senddata = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					var revData = result.msg.split(";");
					if(currid == AddFlag && revData[0] === "success") {
						$.messager.alert("提示", "新建保存成功");
						currPage = revData[1];
						currid = revData[1];
					}else if(revData[0] === "success"){
						$.messager.alert("提示", "修改保存成功");
					}else{
						$.messager.alert("错误", "保存不成功");
					}
				},
				error: function(result) {
					$.messager.alert("错误", "保存不成功");
				}
			});
		}

		function getTotalCharge(totalData) {
			//得到总价
			var sum = parseFloat(totalData.RENT_SUM) + parseFloat(totalData.MARGIN_SUM) + parseFloat(totalData.OTHERS_SUM);
			return sum;
		}
	</script>

</html>